﻿/**
 * 表单的保存功能，如果模型是新的就创建，否则就修改。
 */
Ext.define('Happy.form.action.Save', {
    extend: 'Happy.Action',
    alias: 'action.save',
    requires: [
        'Happy.Ajax',
        'Happy.Msg'
    ],

    /**
     * @inheritdoc
     */
    defaultConfig: {
        itemId: 'save',
        iconCls: 'save-button',
        text: '保存',
        remoteSave: true,
        formSelector: 'form'
    },

    /**
     * @inheritdoc
     */
    onClickHandler: function (button) {
        var me = this;

        var form = me.up(button, me.initialConfig.formSelector);

        if (!form.isValid()) {
            return;
        }

        var record = form.getRecord();

        // 只有当记录没有关联 store 且没有存在于远程服务器的时候，才执行添加逻辑。
        if (record.phantom && !record.store) {
            var rawRecord = record.copy();
            form.updateRecord(record);
            me.createRecord(form, record, rawRecord);
        }
        else {
            form.updateRecord(record);
            me.updateRecord(form, record);
        }
    },

    /**
     * 创建记录。
     * 
     * @private
     * @param {Ext.form.Panel} form
     * @param {Ext.data.Model} record
     * @param {Ext.data.Model} rawRecord
     */
    createRecord: function (form, record, rawRecord) {
        var me = this;

        if (me.initialConfig.remoteSave === true) {
            var createOption = {
                success: function (record_i) {
                    form.loadRecord(rawRecord);
                    Happy.Msg.showSaveSuccess();
                    form.fireEvent('remotecreatesuccess', form, record_i);
                },
                failure: function (record_i, operation) {
                    Happy.Msg.showSaveFailure(operation.error);
                }
            };

            form.fireEvent('beforeremotecreate', form, record, createOption);
            Happy.Ajax.save(record, createOption);
        }
        else {
            Happy.Msg.showSaveSuccess();
            form.loadRecord(rawRecord);
            form.fireEvent('localcreatesuccess', form, record);
        }
    },

    /**
     * 修改记录。
     * 
     * @private
     * @param {Ext.form.Panel} form
     * @param {Ext.data.Model} record
     */
    updateRecord: function (form, record) {
        var me = this;

        if (me.initialConfig.remoteSave === true) {
            var updateOption = {
                success: function (record_i, operation) {
                    //因为有些值是在服务器从新赋值的，如：版本字段，所以要从新更新到表单。
                    form.loadRecord(record_i);
                    Happy.Msg.showSaveSuccess();
                    form.fireEvent('remoteupdatesuccess', form, record_i);
                },
                failure: function (record_i, operation) {
                    Happy.Msg.showSaveFailure(operation.error);
                }
            };

            form.fireEvent('beforeremoteupdate', form, record, updateOption);
            Happy.Ajax.save(record, updateOption);
        }
        else {
            Happy.Msg.showSaveSuccess();
            form.fireEvent('localupdatesuccess', form, record);
        }
    }
});